package com.lgr.store.controller;
import com.lgr.store.service.EmpService;
import org.apache.ibatis.annotations.Param;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgr.store.service.EmpService;
import com.lgr.store.utils.AesUtils;
import com.lgr.store.utils.AppointStateEnum;
import com.lgr.store.utils.LogJournal;
import com.lgr.store.utils.PageObject;
import com.lgr.store.vo.DepVo;
import com.lgr.store.vo.EmpVo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin/emp")
public class EmpController {
    @Resource
    EmpService empService;
    @Resource
    HttpSession session;
    
    /*
     *新增或者修改员工表
     * @param emp 员工表
     * @param cardNo 身份证号
     * @param phone 手机号
     * @return msg 提示信息
     * @return list 员工表列表
     **/
    @RequestMapping("/save")
    public String add(HttpServletRequest request, EmpVo emp, ModelMap map) {
        if (emp.getEmpId() != null && !emp.getEmpId().equals("")) {
            //修改
            empService.update(emp);
            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(2,emp));
        } else {
            //新增
            emp.setPassword(AesUtils.encryptStr("123456", AesUtils.SECRETKEY));
            emp.setStatus(1);
            empService.add(emp);
            session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(1,emp));
        }
        PageObject pager = new PageObject();
        pager.setCur_page(1);
        emp = new EmpVo();
        return list(request, emp,pager, map);
    }

    /*
     *删除一条员工数据把与其有关的全部删除
     * @param empId 员工表主键
     * @return list 员工表列表
     **/
    @RequestMapping("/del")
    public String delete(HttpServletRequest request, String empId, ModelMap map) {
        empService.delete(empId);
        EmpVo empVo=new EmpVo();
        empVo.setEmpId(empId);
        session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(2,empVo));
        PageObject pager = new PageObject();
        pager.setCur_page(1);
        //重新设置查询条件
        EmpVo emp = new EmpVo();
        return list(request, emp, pager, map);
    }

    /*
     *验证初始密码
     * @param password2 验证密码
     * @return pwd 判断状态:0成功,1失败
     * @return msg 提示信息
     **/
    @RequestMapping("/setPwd")
    @ResponseBody
    public Map setPwd(String password2) {
        Map map = new HashMap();
        String pwd = (AesUtils.encryptStr(password2, AesUtils.SECRETKEY));
        EmpVo emp = (EmpVo) session.getAttribute(AppointStateEnum.EMPS.getStateInfo());
        if (pwd.equals(emp.getPassword())) {
            map.put("pwd", 0);
            map.put("msg", "验证密码成功");
        } else {
            map.put("pwd", 1);
            map.put("msg", "验证密码错误");
        }
        return map;
    }

    /*
     * 修改密码后重新登录
     * @param password 修改的密码
     * @return login jsp页面
     * */
    @RequestMapping("/login")
    public String login(String password) {
        String pword = (AesUtils.encryptStr(password, AesUtils.SECRETKEY));
        EmpVo emp = (EmpVo) session.getAttribute(AppointStateEnum.EMPS.getStateInfo());
        emp.setPassword(pword);
        empService.updatePartFiled(emp);
        session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(6,emp));
        session.removeAttribute(AppointStateEnum.EMPS.getStateInfo());
        return "login";
    }

    /*
     * 修改密码的页面
     * @return updatePartFiled jsp页面
     * */
    @RequestMapping("/pwd")
    public String pwd() {
        return "updatePartFiled";
    }

    /*
     *修改状态
     * @param emp 员工表
     * @return list 员工表列表
     * */
    @RequestMapping("/status")
    public String status(HttpServletRequest request, EmpVo emp, ModelMap map) {
        empService.updateStatus(emp);
        //获取登录成功后的用户信息，用户信息保存在session中
        session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(9,emp));
        PageObject pager = new PageObject();
        pager.setCur_page(1);
        emp=new EmpVo();
        return list(request, emp,pager, map);
    }
    /*
     *查询手机号是否存在
     * @param phone 手机号
     * @return ph 判断是否存在:0 不存在,1 已存在
     * @return msg 提示信息
     **/
    @RequestMapping(value = "/getPhone")
    @ResponseBody
    public Map getPhone(String phone,String empId) {
        Map map = new HashMap();
        if(empId!=null){
            int cnt=empService.getPhone(phone,empId);
            if(cnt>0){
                map.put("ph",1);
            }
        }
        return map;
    }
    /*
    * 初始化密码
    * @param emp 员工表
    *
    * */
    @RequestMapping("/szpassword")
    public String updatePassword(HttpServletRequest request,String empId,ModelMap map){
        String pwd="123456";
        //对密码进行加密
        String pwdd=AesUtils.encryptStr(pwd,AesUtils.SECRETKEY);
        EmpVo emp=empService.findById(empId);
        emp.setPassword(pwdd);
        empService.updatePartFiled(emp);
        session.setAttribute(AppointStateEnum.LOG.getStateInfo(),LogJournal.setLogParam(8,emp));
        PageObject pager = new PageObject();
        pager.setCur_page(1);
        emp=new EmpVo();
        return list(request,emp,pager,map);
    }

    /*
     *员工表列表
     * @param emp 员工表
     * @param pager 分页变量
     * @return emp 搜索表单数据
     * @return pager 员工表数据
     * @return empList jsp页面
     * */
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request, EmpVo emp,PageObject pager,ModelMap map) {
        if(emp.getEmpName()==null){
            emp.setStatus(-1);
        }
        int cnt = 0;
        //保存查询条件到session,供刷新页面，点击分页按钮时读取查询条件
        if (emp.getEmpName()==null) {//没有条件时从session取值
            if (request.getSession().getAttribute("emp") != null) {
                emp = (EmpVo) request.getSession().getAttribute("emp");
            }
        }else{//如果有查询条件就保存到session中
            request.getSession().setAttribute("emp", emp);
        }

        EmpVo emp2= (EmpVo) session.getAttribute(AppointStateEnum.EMPS.getStateInfo());
        String empId=emp2.getEmpId();
        //查询是否为部门负责人
        String depfzr=emp2.getDep().getChairman();
        boolean fzr=false;
        if (empId.equals(depfzr)){
            fzr=true;
        }
        map.addAttribute("fzr",fzr);
        cnt = empService.count(emp,emp2);
        pager.setTotalRows(cnt);
        List<EmpVo> empList = empService.list(emp,emp2,pager);

        List<DepVo> listDep = empService.listDep();
        DepVo depVo=new DepVo();
        depVo.setDepid(0);
        depVo.setDepName("--未选择--");
        listDep.add(0,depVo);

        map.addAttribute("listDep", listDep);
        map.addAttribute("emp", emp);
        pager.setDatas(empList);
        map.addAttribute("pager", pager);
        return "empList";
    }
    /*
     *获取一条员工信息
     * @param empId 员工表主键
     * @return listDep 部门数据
     * @return emp 员工数据
     * @return empAdd jsp页面
     **/
    @RequestMapping("/getById")
    public String getById(@RequestParam(value = "empId") String empId, Model model, ModelMap map) {
        EmpVo emp = empService.findById(empId);
        EmpVo emp2= (EmpVo) session.getAttribute(AppointStateEnum.EMPS.getStateInfo());
        List<DepVo> listDep = empService.listDep();
        map.addAttribute("listDep", listDep);
//        List<PostVo> listPost=empService.listPost();
//        map.addAttribute("listPost",listPost);
        model.addAttribute("emp", emp);
        return "empAdd";
    }

    /*
     *新增一条员工信息
     * @return listDep 部门数据
     * @return emp 员工数据
     * @return empAdd jsp页面
     **/
    @RequestMapping("/add")
    public String add(ModelMap map) {
        EmpVo emp2= (EmpVo) session.getAttribute(AppointStateEnum.EMPS.getStateInfo());
        List<DepVo> listDep = empService.listDep();
        map.addAttribute("listDep", listDep);
//        List<PostVo> listPost=empService.listPost();
//        map.addAttribute("listPost",listPost);
        map.addAttribute("emp", null);
        return "empAdd";
    }
    /*
     * 获取一条员工表数据
     * @param empId 员工表主键
     * @return emp 返回一条json数据
     * */
    @RequestMapping("/json")
    @ResponseBody
    public Map json(@RequestParam(value = "empId") String empId) {
        EmpVo emp = new EmpVo();
        emp = empService.findById(empId);
        Map map = new HashMap();
        map.put("emp", emp);
        return map;
    }
}
